/*
Copyright 2007 Julian Asamer

This file is part of Fractalicious.

    Fractalicious is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Fractalicious is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Fractalicious.  If not, see <http://www.gnu.org/licenses/>.
*/

//Boring stuff - the main view.
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIPreferencesTableCell.h>
#import <UIKit/UIPreferencesTable.h>
#import "FractaliciousApplication.h"
#import "FView.h"
#import "SettingsView.h"
#import "FractalController.h"
#import "Vector.h"
#import "DB.h"

static FractaliciousApplication *sharedInstance;

@implementation FractaliciousApplication

+ (FractaliciousApplication *)sharedInstance {
	if (!sharedInstance)
		sharedInstance = [[FractaliciousApplication alloc] init];
	
	return sharedInstance;
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table {
	return 1;
}
- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group {
	if (group==0) {
		return [cells count];//-1;
	} else {
		return 1;
	}
}
- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group {
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	return [cell autorelease];
}
- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group {
	return [cells objectAtIndex: (row+([cells count] - 1)*group)]; //:-)
}
- (float)preferencesTable:(UIPreferencesTable *)aTable heightForRow:(int)row inGroup:(int)group withProposedHeight:(float)proposed {
	return proposed;
}

- (void)tableRowSelected:(NSNotification*)notification {
	//NSLog(@"selected: %i count: %i",[homeTable selectedRow],[cells count]);
	// if ([homeTable selectedRow]==([cells count]+1)) {
	// 	[self showSettings];
	// } else {
		[self setStatusBarMode: 2 duration: 0];
		[window setFrame: CGRectMake(0,0,320,480)];
		[mainView setFrame: CGRectMake(0,0,320,480)];
		FractalDescription * fractal = [[DB getFractals] objectAtIndex: [homeTable selectedRow] -1];
		[mainView transition: 1 toView: [[FractalController sharedInstance] getView]];
		[[FractalController sharedInstance] setFractal: fractal];
	//}
}

- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button {
	switch (button) 
	{
		case 0: {
			[nav showButtonsWithLeftTitle: nil rightTitle: nil leftBack: YES];
			[tableView transition: 2 toView: homeTable];
			[nav popNavigationItem];
			break;
		} 
		case 1:{
			[nav showButtonsWithLeftTitle: nil rightTitle: nil leftBack: YES];
			[tableView transition: 2 toView: homeTable];
			[nav popNavigationItem];
			break;
		}
	}
}

- (void)moveBackFromFractal {
	[self setStatusBarMode: 1 duration: 0.];
	[window setFrame: [UIHardware fullScreenApplicationContentRect]];
	[mainView transition: 2 toView: tableNav]; 
}

- (void)showSettings {
	SettingsView *sview = [[SettingsView alloc] init];
	[sview reloadData];
	[tableView transition: 1 toView: sview];
	UINavigationItem *navItem3 = [[UINavigationItem alloc] initWithTitle: @"Settings"];
	[nav pushNavigationItem: navItem3];
	[nav showButtonsWithLeftTitle:@"Save and back" rightTitle:@"Cancel" leftBack: YES];
}

- (void) addCellWithTitle: (id) string {
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	[cell setTitle: string]; 
	[cell setDisclosureStyle: 2];
	[cell setShowDisclosure: YES];
	[cells addObject: cell];
}

- (void) applicationDidFinishLaunching: (id) unused {
	
	sharedInstance = self;
	
	//Init the table cells
	cells = [[NSMutableArray alloc] initWithCapacity:2];

	NSMutableArray *fractals = [DB getFractals];
	int i;
	for (i=0; i< [fractals count]; i++) 
		[self addCellWithTitle: [[fractals objectAtIndex: i] getName]];
	
	//[self addCellWithTitle: @"Settings"];

	window = [[UIWindow alloc] initWithContentRect: [UIHardware
		fullScreenApplicationContentRect]];

	homeTable = [[UIPreferencesTable alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 
		320.0f, 480.0f - 16.0f - 32.0f)];

	[window orderFront: self];
	[window makeKey: self];
	[window _setHidden: NO];
	[homeTable setDataSource: self];
	[homeTable setDelegate: self];
	[homeTable reloadData];


	tableNav = [[UIView alloc] initWithFrame: CGRectMake(
		0.0f, 0.0f, 320.0f, 480.0f)];
	nav = [[UINavigationBar alloc] initWithFrame: CGRectMake(
		0.0f, 0.0f, 320.0f, 48.0f)];
	[nav setBarStyle: 0];
	[nav setDelegate: self];
	UINavigationItem *navItem = [[UINavigationItem alloc] 
		initWithTitle: @"Fractalicious"];
	
	[nav pushNavigationItem: navItem];

	tableView = [[UITransitionView alloc] initWithFrame: CGRectMake(0.0f, 48.0f,
        320.0f, 480.0f - 16.0f - 32.0f)];
	[tableView addSubview: homeTable];

    struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0.0f;

    mainView = [[UITransitionView alloc] initWithFrame: rect];
    [tableNav addSubview: nav]; 
  	[tableNav addSubview: tableView];
	[mainView addSubview: tableNav];

    [window setContentView: mainView]; 
}

@end